package com.dgz.springboot18.controller;

import com.dgz.springboot18.pojo.StudentInfo;
import com.dgz.springboot18.pojo.StudentTask;
import com.dgz.springboot18.pojo.TaskInfo;
import com.dgz.springboot18.pojo.vo.StuAndTask;
import com.dgz.springboot18.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class TaskController {

    @Autowired
    private TaskService taskService;


    /**
     * 添加任务
     * @param taskInfo
     * @return
     */
    @PutMapping("/task")
    public Map<String,Object> addTask(TaskInfo taskInfo){
        System.out.println(taskInfo);
        Map<String,Object> map=new HashMap<>();
        TaskInfo result = taskService.addTask(taskInfo);
        map.put("code",0);
        map.put("msg","添加成功");
        map.put("data",result);
        return map;
    }

    /**
     * 根据id删除任务
     * @param id
     * @return
     */
    @DeleteMapping("/task")
    public Map<String,Object> delTask(String id){
        Map<String,Object> map=new HashMap<>();
        taskService.delTask(id);
        map.put("code",0);
        map.put("msg","删除成功");
        return map;
    }

    /**
     * 根据id修改任务
     * @param taskInfo
     * @return
     */
    @PostMapping("/task")
    public Map<String,Object> delTask(TaskInfo taskInfo){
         Map<String,Object> map=new HashMap<>();
        TaskInfo result = taskService.updTask(taskInfo);
        map.put("code",0);
        map.put("msg","修改成功");
        map.put("data",result);
        return map;
    }

    /**
     * 根据id查询任务
     * @param id
     * @return
     */
    @GetMapping("/task/{id}")
    public Map<String,Object> queryTask(@PathVariable String id){
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","查询成功");
        map.put("data",taskService.queryTask(id));
        return map;
    }

    /**
     * 全查询任务,并且分页
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/tasks")
    public Map<String,Object> queryTasks(Integer page,Integer limit){
        Map<String,Object> map=new HashMap<>();
        Page<TaskInfo> result = taskService.queryTasks(page, limit);
        map.put("code",0);
        map.put("msg","查询成功");
        map.put("data",result.getContent());
        map.put("count",result.getTotalElements());
        return map;
    }


    /**
     * 查询最新任务的学生打卡信息
     */
    @GetMapping("/queryLast")
    public Map<String,Object> queryLast(Integer page,Integer limit) {
        Map<String,Object> map=new HashMap<>();
        Page<StudentTask> studentTasks = taskService.queryLast(new StudentTask(), page, limit);
        List<StuAndTask> stuAndTasks=new ArrayList<>();
        for (StudentTask studentTask : studentTasks.getContent()) {
            StudentInfo studentInfo = taskService.queryStu(studentTask.getStuId());
            TaskInfo taskInfo = taskService.queryTask(studentTask.getTaskId());
            StuAndTask stuAndTask=new StuAndTask();
            stuAndTask.setStuId(studentInfo.getStuId());
            stuAndTask.setStuName(studentInfo.getStuName());
            stuAndTask.setTaskName(taskInfo.getTaskName());
            stuAndTask.setTaskContent(taskInfo.getTaskContent());
            stuAndTask.setTaskAdmin(taskInfo.getTaskAdmin());
            stuAndTask.setTaskAndTime(taskInfo.getTaskStartTime());
            stuAndTask.setFlagTask(studentTask.getFlagTask());
            if (studentTask.getFlagTask()) {
                stuAndTask.setIsTime(studentTask.getIsTime());
            }
            stuAndTasks.add(stuAndTask);
        }
        map.put("code",0);
        map.put("msg","查询成功");
        map.put("data",stuAndTasks);
        map.put("count",studentTasks.getTotalElements());
        return map;

    }

     /**
     * 根据任务id的学生打卡信息
     */
    @GetMapping("/queryLastById")
    public Map<String,Object> queryLast(Integer taskId,Integer page,Integer limit) {
        Map<String,Object> map=new HashMap<>();
        StudentTask student=new StudentTask();
        student.setTaskId(String.valueOf(taskId));
        Page<StudentTask> studentTasks = taskService.queryLastById(student, page, limit);
        List<StuAndTask> stuAndTasks=new ArrayList<>();
        for (StudentTask studentTask : studentTasks.getContent()) {
            StudentInfo studentInfo = taskService.queryStu(studentTask.getStuId());
            TaskInfo taskInfo = taskService.queryTask(studentTask.getTaskId());
            StuAndTask stuAndTask=new StuAndTask();
            stuAndTask.setStuId(studentInfo.getStuId());
            stuAndTask.setStuName(studentInfo.getStuName());
            stuAndTask.setTaskName(taskInfo.getTaskName());
            stuAndTask.setTaskContent(taskInfo.getTaskContent());
            stuAndTask.setTaskAdmin(taskInfo.getTaskAdmin());
            stuAndTask.setTaskAndTime(taskInfo.getTaskStartTime());
            stuAndTask.setFlagTask(studentTask.getFlagTask());
            if (studentTask.getFlagTask()) {
                stuAndTask.setIsTime(studentTask.getIsTime());
            }
            stuAndTasks.add(stuAndTask);
        }
        System.out.println(stuAndTasks);
        map.put("code",0);
        map.put("msg","查询成功");
        map.put("data",stuAndTasks);
        map.put("count",studentTasks.getTotalElements());
        return map;

    }



}
